package com.zhanghu.backendZray.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhanghu.backendZray.mapper.DouyinHotMapper;
import com.zhanghu.backendZray.pojo.dto.douyinHot.DouyinHotDTO;
import com.zhanghu.backendZray.pojo.entity.DouyinHot;
import com.zhanghu.backendZray.pojo.vo.douyin.DouyinHotVo;
import com.zhanghu.backendZray.service.DouyinHotService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 抖音热点 服务实现类
 * </p>
 *
 * @author zhangdong
 * @since 2025-01-20
 */
@Service
@Slf4j
public class DouyinHotServiceImpl extends ServiceImpl<DouyinHotMapper, DouyinHot> implements DouyinHotService {

    /**
     * 获取抖音热点
     */
    @Override
    public List<DouyinHotVo> getHot() {
        List<DouyinHot> list = list();
        List<DouyinHotVo> voList = list.stream().map(r -> {
            DouyinHotVo vo = new DouyinHotVo();
            vo.setWord(r.getWord());
            vo.setHotValue(r.getHotValue());
            return vo;
        }).collect(Collectors.toList());
        return voList;
    }

    /**
     * 删除所有并保存新数据
     */
    @Transactional(rollbackFor = Exception.class)
    public void removeAllAndSave(List<DouyinHotDTO> list) {
        baseMapper.removeAll();
//        getBaseMapper().delete(null);
        List<DouyinHot> douyinHotList = list.stream().map(douyinHotDTO -> {
            DouyinHot douyinHot = new DouyinHot();
            douyinHot.setWord(douyinHotDTO.getWord());
            douyinHot.setHotValue(douyinHotDTO.getHotValue());
            return douyinHot;
        }).collect(Collectors.toList());
        saveBatch(douyinHotList);
    }
}
